﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class ResourceMgr        //선언
{
    //SingleTon
    private static ResourceMgr instance;
    private static GameObject container;

    public static ResourceMgr GetInstance()
    {
        if (!instance)
        {
            container = new GameObject();
            container.name = "ResourceMgr";
            instance = container.AddComponent(typeof(ResourceMgr)) as ResourceMgr;
        }
        return instance;
    }



    //Effect

    //BulletMark
    private GameObject m_pBulletMark;

    private Sprite m_pBulletMarkSprite_0;
    private Sprite m_pBulletMarkSprite_1;
    private Sprite m_pBulletMarkSprite_2;

    //Smoke
    public GameObject m_MarkSmoke;


    //Blood
    private GameObject m_pBloodMark;

    private Sprite m_BloodMarkSprite_0;
    private Sprite m_BloodMarkSprite_1;
    private Sprite m_BloodMarkSprite_2;
    private Sprite m_BloodMarkSprite_3;

    public GameObject m_objBloodSpread;


    //GameUI
    public GameObject m_pSelectUI = null;
    public GameObject m_pMoveLine = null;
    public GameObject m_pMoveSight = null;

    public GameObject m_pDestPoint = null;
    public GameObject m_pInputUI = null;

    public GameObject m_CSUI = null;
    public GameObject m_pHoverUnitInformUI = null;
    public GameObject m_pInGameText = null;
    public GameObject m_pChatBubble = null;
    public GameObject m_pSystemMsg = null;
    public GameObject m_pInformBox = null;
    public GameObject m_pNameTag = null;
    
    public GameObject m_pEventMsg = null;
    public GameObject m_pTimedEvent = null;

    public List<GameObject> m_pBoundaryLineList = null;
}




public partial class ResourceMgr : MonoBehaviour
{
    public void ResourcesLoad()
    {
        //BulletMark
        m_pBulletMark = Resources.Load("Prefaps/Effect/BulletMark") as GameObject;

        m_pBulletMarkSprite_0 = Resources.Load("Sprite/Effect/BulletMark/BulletMark_0.png") as Sprite;
        m_pBulletMarkSprite_1 = Resources.Load("Sprite/Effect/BulletMark/BulletMark_1.png") as Sprite;
        m_pBulletMarkSprite_2 = Resources.Load("Sprite/Effect/BulletMark/BulletMark_2.png") as Sprite;

        //Smoke
        m_MarkSmoke = Resources.Load("Prefaps/Effect/BulletSmoke") as GameObject;

        //Blood
        m_pBloodMark = Resources.Load("Prefaps/Effect/BloodMark") as GameObject;
        m_objBloodSpread = Resources.Load("Prefaps/Effect/BloodSpread") as GameObject;

        m_BloodMarkSprite_0 = Resources.Load<Sprite>("Sprite/Effect/BloodMark/BloodkMark_0.png");
        m_BloodMarkSprite_1 = Resources.Load<Sprite>("Sprite/Effect/BloodMark/BloodkMark_1.png");
        m_BloodMarkSprite_2 = Resources.Load<Sprite>("Sprite/Effect/BloodMark/BloodkMark_2.png");
        m_BloodMarkSprite_3 = Resources.Load<Sprite>("Sprite/Effect/BloodMark/BloodkMark_3.png");


        //GameUI
        m_pInputUI = Resources.Load("Prefaps/GameUI/InputUI") as GameObject;
        m_pSelectUI = Resources.Load("Prefaps/GameUI/SelectUI") as GameObject;
        m_pMoveLine = Resources.Load("Prefaps/GameUI/Line") as GameObject;
        m_pDestPoint = Resources.Load("Prefaps/GameUI/DestPoint") as GameObject;
        m_pMoveSight = Resources.Load("Prefaps/GameUI/MoveSight") as GameObject;

        m_CSUI = Resources.Load("Prefaps/GameUI/CS_UI") as GameObject;

        m_pHoverUnitInformUI = Resources.Load("Prefaps/GameUI/HoverUnitInformUI") as GameObject;
        m_pInGameText = Resources.Load("Prefaps/GameUI/InGameText") as GameObject;
        m_pChatBubble = Resources.Load("Prefaps/GameUI/ChatBubble") as GameObject;
        m_pSystemMsg = Resources.Load("Prefaps/UI/StaticUI/SystemMsg/SystemMsg") as GameObject;
        m_pInformBox = Resources.Load("Prefaps/UI/StaticUI/Inform/Inform") as GameObject;
        m_pNameTag = Resources.Load("Prefaps/UI/StaticUI/NameTag/NameTag") as GameObject;

        m_pEventMsg = Resources.Load("Prefaps/UI/StaticUI/Event/EventMsg") as GameObject;
        m_pTimedEvent = Resources.Load("Prefaps/UI/StaticUI/Event/TimedEvent") as GameObject;



        //m_pBoundaryLineList = new List<GameObject>();

    }

    public GameObject GetBloodMark()
    {
        int iFlag = Random.Range(0, 3);
        Sprite pSprite = null;

            switch (iFlag)
            {
                case 0:
                    pSprite = m_BloodMarkSprite_0;
                    break;
                case 1:
                    pSprite = m_BloodMarkSprite_1;
                    break;
                case 2:
                    pSprite = m_BloodMarkSprite_2;
                    break;
                case 3:
                    pSprite = m_BloodMarkSprite_3;
                    break;
                default:
                    pSprite = m_BloodMarkSprite_0;
                    break;

            }

            //m_pBloodMark.GetComponent<SpriteRenderer>().sprite = pSprite;
            return m_pBloodMark;
    }

    public GameObject GetBulletMark()
    {
        int iFlag = Random.Range(0, 2);
        Sprite pSprite = null;

        switch (iFlag)
        {
            case 0:
                pSprite = m_pBulletMarkSprite_0;
                break;
            case 1:
                pSprite = m_pBulletMarkSprite_1;
                break;
            case 2:
                pSprite = m_pBulletMarkSprite_2;
                break;
            default:
                pSprite = m_pBulletMarkSprite_0;
                break;

        }

        //m_pBulletMark.GetComponent<SpriteRenderer>().sprite = pSprite;
        return m_pBulletMark;
    }

    //GameUI
    public GameObject CreateSelectUI(GameObject pOwner)
    {
        GameObject pSelectUI_Clone;

        pSelectUI_Clone = Instantiate(m_pSelectUI, pOwner.transform.position, Quaternion.identity) as GameObject;  //Create SelectUI
        pSelectUI_Clone.GetComponent<SelectUI>().m_pOwner = pOwner;
        pSelectUI_Clone.transform.parent = pOwner.transform.parent.parent.GetChild(1);

        return pSelectUI_Clone;
    }

    public GameObject CreateHoverUnitInformUI(GameObject pOwner)
    {
        if (GameObject.Find("HoverUI").transform.childCount == 0)
        {
            GameObject pHoverUnitInformUI_Clone;

            pHoverUnitInformUI_Clone = Instantiate(m_pHoverUnitInformUI, pOwner.transform.position, Quaternion.identity) as GameObject;
            pHoverUnitInformUI_Clone.GetComponent<HoverUnitInformUI>().m_pOwner = pOwner;
            pHoverUnitInformUI_Clone.transform.parent = GameObject.Find("HoverUI").transform;

            return pHoverUnitInformUI_Clone;
        }
        else
            return null;
    }

    public GameObject CreateInGameText(GameObject pOwner, string strText, Color color, string strSpriteName = "")
    {
        GameObject m_pInGameText_Clone;

        m_pInGameText_Clone = Instantiate(m_pInGameText, pOwner.transform.position, Quaternion.identity) as GameObject;
        m_pInGameText_Clone.GetComponent<UIFollowTarget>().enabled = true;
        m_pInGameText_Clone.GetComponent<InGameText>().m_pOwner = pOwner;
        m_pInGameText_Clone.GetComponent<InGameText>().m_strText = strText;
        m_pInGameText_Clone.transform.GetChild(0).GetChild(0).GetComponent<UILabel>().color = color;

        if (strSpriteName != "")
        {
            m_pInGameText_Clone.transform.GetChild(0).GetChild(1).GetComponent<UISprite>().enabled = true;
            m_pInGameText_Clone.transform.GetChild(0).GetChild(1).GetComponent<UISprite>().spriteName = strSpriteName;
        }

        m_pInGameText_Clone.transform.parent = GameObject.Find("TextUI").transform;

        return m_pInGameText;
    }

    public void CreateChatBubble(GameObject pOwner, CHATBUBBLE_STATE chatState, int sceneID = 0, int Log_Num = 0)  //Scene ID 는 EventDialog.xml 참조
    {
        GameObject m_pChatBubble_Clone;

        m_pChatBubble_Clone = Instantiate(m_pChatBubble, pOwner.transform.position, Quaternion.identity) as GameObject;  
        m_pChatBubble_Clone.GetComponent<ChatBubble>().m_pOwner = pOwner;
        switch (chatState)
        {
            case CHATBUBBLE_STATE.NORMAL:
                xmlLoader.GetInstance().m_XML_UnitSpeech[pOwner.GetComponent<UnitStatus>().m_unitData.iChaos / 10].TryGetValue(string.Format("normal_{0}", Random.Range(0, 3)), out m_pChatBubble_Clone.GetComponent<ChatBubble>().m_strText);
                break;

            case CHATBUBBLE_STATE.ATTACK:
                xmlLoader.GetInstance().m_XML_UnitSpeech[pOwner.GetComponent<UnitStatus>().m_unitData.iChaos / 10].TryGetValue(string.Format("attack_{0}", Random.Range(0, 5)), out m_pChatBubble_Clone.GetComponent<ChatBubble>().m_strText);
                break;

            case CHATBUBBLE_STATE.EVENT:
                xmlLoader.GetInstance().m_XML_EventLogs[sceneID].TryGetValue(string.Format("Log_{0}", Log_Num), out m_pChatBubble_Clone.GetComponent<ChatBubble>().m_strText);
                break;
        }
        m_pChatBubble_Clone.transform.parent = GameObject.Find("TextUI").transform;
    }

    public GameObject CreateNameTag(GameObject pOwner)
    {
        GameObject m_pNameTag_Clone;

        m_pNameTag_Clone = Instantiate(m_pNameTag, pOwner.transform.position, Quaternion.identity) as GameObject;  
        m_pNameTag_Clone.GetComponent<NameTag>().m_pOwner = pOwner;
        m_pNameTag_Clone.transform.parent = GameObject.Find("NameUI").transform;

        return m_pNameTag_Clone;
    }

    public void CreateSystemMsg(string strMsg)
    {
        GameObject m_pSystemMsg_Clone;

        m_pSystemMsg_Clone = Instantiate(m_pSystemMsg) as GameObject;
        m_pSystemMsg_Clone.transform.parent = GameObject.Find("SystemMsgs").transform;
        m_pSystemMsg_Clone.GetComponent<SystemMsg>().m_iMsgIndex = GameObject.Find("SystemMsgs").transform.childCount -1;
        m_pSystemMsg_Clone.GetComponent<SystemMsg>().m_strMsg = strMsg;

    }

    public void CreateEventMsg(string evnetID)
    {
        GameObject m_pEventMsg_Clone;
        m_pEventMsg_Clone = Instantiate(m_pEventMsg) as GameObject;
        m_pEventMsg_Clone.transform.parent = GameObject.Find("EventMsgs").transform;
        m_pEventMsg_Clone.GetComponent<EventMsg>().m_strTimedEventID = evnetID;
    }

    public void CreateTimedEvent(string evnetID)
    {
        GameObject m_pTimedEvent_Clone;
        m_pTimedEvent_Clone = Instantiate(m_pTimedEvent) as GameObject;
        m_pTimedEvent_Clone.transform.parent = GameObject.Find("EventMgr").transform;
        m_pTimedEvent_Clone.GetComponent<TimedEvent>().strEventID = evnetID;
    }

    public void CreateInformBox(string strName, string strDesc)
    {
        GameObject m_pInformBox_Clone;

        m_pInformBox_Clone = Instantiate(m_pInformBox) as GameObject;
        m_pInformBox_Clone.transform.parent = GameObject.Find("InformBoxUI").transform;
        m_pInformBox_Clone.GetComponent<Inform>().m_strDesc = strDesc;
        m_pInformBox_Clone.GetComponent<Inform>().m_strName = strName;

    }

    public List<GameObject> DrawBoundaryUnitLine(GameObject Structure, List<GameObject> LineList, float fDistance ,Color color)
    {
        for (int j = 0; j < LineList.Count; ++j)
        {
            if (LineList[j])
                GameObject.Destroy(LineList[j]);
        }

        for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.Count; ++i)
        {
            if (Vector3.Distance(Structure.transform.position, GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList[i].gameObject.transform.position) < fDistance)
            {
                LineList.Add(GameObject.Instantiate(ResourceMgr.GetInstance().m_pMoveLine, Structure.transform.position, Quaternion.identity) as GameObject);
                LineList[LineList.Count - 1].GetComponent<Line>().SetLine(Structure.transform.position, GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList[i].gameObject.transform.position);
                LineList[LineList.Count - 1].GetComponent<Line>().m_pOwner = Structure;
                LineList[LineList.Count - 1].GetComponent<SpriteRenderer>().color = color;
            }
        }

        return LineList;
    }

    public GameObject Get_DestPoint()
    {
        return m_pDestPoint;
    }

    public GameObject Get_MoveSight()
    {
        return m_pMoveSight;
    }

    void Update()
    {

    }
}
